import requests,re,json
from requests.exceptions import RequestException



def getPage(url):
	headers={
	'Referer': 'http://maoyan.com/board/4',
	'Upgrade-Insecure-Requests': '1',
	'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.117 Safari/537.36'
	}
	try:
		req=requests.get(url,headers=headers)
		content=req.content.decode('utf-8')
		return content		
	except RequestException as e:
		print('---error---',e)
		return None

def  parsePage(content):
	paiming=re.compile('<i class="board-index board-index-\w*">(.*?)</i>',re.S)
	poster=re.compile('data-src="(.*?)"',re.S)
	star=re.compile('<p class="star">(.*?)</p>',re.S)
	name=re.compile('<p class="name"><a.*?data-act="boarditem-click".*?>(.*?)</a></p>',re.S)
	time=re.compile('<p class="releasetime">(.*?)</p>',re.S)
	score=re.compile('<p class="score"><i class="integer">(.*?)</i><i class="fraction">(.*?)</i></p>',re.S)

	paiming=paiming.findall(content)
	poster=poster.findall(content)
	star=star.findall(content)
	name=name.findall(content)
	time=time.findall(content)
	score=score.findall(content)
	content_list=[]
	for i in range(10):
		filmObj={
		'paiming':paiming[i],
		'poster':poster[i],
		'star':star[i],
		'name':name[i],
		'time':time[i],
		'score':score[i][0]+score[i][1]
		}
		content_list.append(filmObj)
		#不转车ascii码
	return json.dumps(content_list,ensure_ascii=False)

def writeJson(result):
	with open('./data.json','a',encoding='utf-8') as f:
		f.write(result)
		f.close()




if __name__ == '__main__':
	for i in range(0,10):
		print('正在获取第'+str(i+1)+'页数据\n')
		url='http://maoyan.com/board/4?offset='+str(i*10)
		content=getPage(url)
		result = parsePage(content)
		writeJson(result)
	print('*'*12,'数据获取成功，查看data.json文件','*'*12)

	
